$(document).ready(
				function() {
	
					//获取项目名
					var contextPath = document.location.pathname; 
					var index =contextPath.substr(1).indexOf("/"); 
					contextPath = contextPath.substr(0,index+1); 
					
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					
					var typeArr = new Array()// 定义一个数组 ,存放父级id

					$("#gityDetails").on('hidden.bs.modal', function() {
						$(this).removeData("bs.modal");
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#giftType").attr("method","post");
										
										var gityId = $("#gityId").val();
									
											if (gityId =="") {
												$("#giftType").attr("action","save.do");
											} else {
												$("#giftType").attr("action","update.do");
											}
											$("#giftType").submit();
										
									});

					$('#pagination2').jqPaginator(
									{
										/* totalPages: pageCount,
										    visiblePages: 10,
										    currentPage: 1,*/
										totalCounts:totalCount,
										pageSize: pSize,
									    visiblePages: pageCount,
									    currentPage: 1,
										
										    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {
															$("#gityList tr:not(:first)").remove();
															typeArr = [];//加载前，情况数组
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var gity = results[i];
																var rowContent = "";
																if(gity.parentId=="" || gity.parentId==null){
																	rowContent = "<tr class='paretsShow' id='" 
																		+ gity.id
																		+ "'>"
																		+ "<td><input name='gityId' id='gityId' class='checkbox' type='checkbox' value='"
																		+ gity.id
																		+ "'/></td>"
																		+ "<td>"
																		+ gity.typeCode
																		+ "</td>"
																		+ "<td>"
																		+ gity.typeName
																		+ "</td>"
																		+ "<td>"
																		+ gity.description
																		+ "</td>"
																		+ "<td>"
																		+ gity.note
																		+ "</td>";
																		
																		rowContent = rowContent
																		+ "<td>" 
																		+ "<button id='viewGiTypeDir' objId='"
																		+ gity.id 
																		+ "' class='btn btn-sm-block' data-toggle='modal' >礼品基本信息</button>" 
																		+ "</td>"
																		
																		+ "<td>" 
																		+ "<button name='editBtn' id='gityBtn' objId='"
																		+ gity.id
																		+ "' class='btn btn-sm-block'>修改</button>" 
																		+ "</td>" 
																		+ "<td>"
																		+ "<button id='gityBtn' name='checkBtn' objId='"
																		+ gity.id
																		+ "' class='btn btn-sm-block btn-sm-info checkBtn'>查看</button></td>"
																		+ "</tr>";
																		typeArr.push(gity.id);//添加到数组
																}
																
																$("#gityList").append(rowContent);
															}
															
															for(y=0; y<typeArr.length; y++){
																var typeParId = typeArr[y];
																ajaxAddSon(typeParId);
															}
														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**查询子级列表*/
					function ajaxAddSon(typeParId){
						var editUrl = "listSon.do?typeParId=" + typeParId;
						$.ajax({
							url : editUrl,
							dataType : 'json',
							async: false,
							data : {
								id : typeParId
							},
							cache : false,
							success : function(dataSon) {
								var typeList = dataSon.typeList;
								for ( j= 0; j < typeList.length; j++) {
									var gitySon = typeList[j];
									var rowContent = "";
									if(gitySon.parentId==typeParId && gitySon.parentId!=null){
										rowContent = "<tr id='hideSon' sonId='"
											+ gitySon.id
											+"'>"
											+ "<td align='right'><input name='gityId' id='gityId' class='checkbox' type='checkbox' value='"
											+ gitySon.id
											+ "'/></td>"
											+ "<td>"
											+ gitySon.typeCode
											+ "</td>"
											+ "<td>"
											+ gitySon.typeName
											+ "</td>"
											+ "<td>"
											+ gitySon.description
											+ "</td>"
											+ "<td>"
											+ gitySon.note
											+ "</td>";
										
										rowContent = rowContent
										+ "<td>" 
										+ "<button id='viewGiTypeDir' objId='"
										+ gitySon.id 
										+ "' class='btn btn-sm-block' data-toggle='modal' >礼品基本信息</button>" 
										+ "</td>"
										
										+ "<td>" 
										+ "<button name='editBtn' id='gityBtn' objId='"
										+ gitySon.id
										+ "' class='btn btn-sm-block'>修改</button>" 
										+ "</td>" 
										+ "<td>"
										+ "<button id='gityBtn' name='checkBtn' objId='"
										+ gitySon.id
										+ "' class='btn btn-sm-block btn-sm-info checkBtn'>查看</button></td>"
										+ "</tr>";
									}
									$("#"+typeParId).after(rowContent);
								}
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});
					}
					
					/**子级显隐*/
					$(".paretsShow").nextUntil(".paretsShow").css("visibility", "visible");
					$("#gityList").delegate(".paretsShow", 'click', function() {
						$(this).nextUntil(".paretsShow").toggle();
					});
						
					
					
					/**
					 * 全选当前页记录
					 */
					$("#gityList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='gityId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});
					
					/**
					 * 修改按钮事件
					 */
					
					$("#gityList").delegate('#gityBtn', 'click', function() {
						$("#checks input").removeAttr("readonly");
						$("#checks textarea").removeAttr("readonly");
//						$("#checks select").removeAttr("disabled");
						
						var gityId = $(this).attr("objId");
						
						var editUrl = "show.do?gityId=" + gityId;
						$.ajax({
							
							url : editUrl,
							dataType : 'json',
							data : {
								id : gityId
							},
							cache : false,
							
							success : function(data) {
								
								loadGI(data);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});
						
					});
					
					
					
					/**
					 * 查看详情
					 */
					$("#gityList").delegate('.checkBtn', 'click', function() {
						
						$("#checks input").attr({ readonly: 'true' });
						$("#checks textarea").attr({ readonly: 'true' });
//						$("#checks select").attr("disabled","disabled");
						
						
					});

					
					/**
					  删除按钮事件
					 */
					$("#tools").delegate(
						'#deleteBtn',
						'click',
						function() {
							delChk = $('input[name="gityId"]:checked');

							if (delChk.length > 0) {
								if (confirm("Are you sure delete the record?")) {

									var ids = "";
									var delUrl = "delete.do";
                                    var idArr=new Array();
                                   
                                    
                                    for (var i = 0; i < delChk.length; i++) {
                                    	idArr.push(delChk.get(i).value);
                               
								}
									
									$.ajax({
												url : delUrl,
												type: "POST",
												data : {
													ids : idArr.toString()
												},
												cache : false,

												success : function(
														html) {
													alert("Delete successfully");
													refresh();
												},
												error : function(
														html) {
													alert("Delete failly");
													refresh();
												}
											});
								} else {
									return;
								}
							} else {
								alert("Please select the record to delete");
								return;
							}
						});
					
					
					/**
					 * 添加
					 */
					$("#tools").delegate('#addgiftType', 'click', function() {
						
						$("#checks textarea").removeAttr("readonly");
						
						$("#gityDetails input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);

						});

						$("#gityDetails textarea").each(function() {
							$(this).prop("value", null);

						});
						
						$("#gityDetails").modal('show');

					});
					
					/**
					 * 修改页面上的默认值
					 */
					function loadGI(giftType) {
						$("#gityId").val(giftType.id);
						$("#parentId").val(giftType.parentId);
						$("#typeCode").val(giftType.typeCode);
						$("#typeName").val(giftType.typeName);
						$("#description").val(giftType.description);
						$("#note").val(giftType.note);

						$("#gityDetails").modal('show');
					}

					function vaildate(form) {
						return true;
					}
					/**
					 * 刷新页面
					 * */
					function refresh() {
						window.location.reload();
					}
				});

